package cheqex.update.controllers;

//parename to CashInBankController

import java.util.Calendar;
import java.util.List;

import org.apache.torque.util.Criteria;

import cheqex.dao.CollectionCashPeer;
import cheqex.dao.CollectionCheckPeer;
import cheqex.dao.DepositCash;
import cheqex.dao.DepositCashPeer;
import cheqex.dao.Report;
import cheqex.dao.ReportPeer;
import cheqex.dao.WithdrawalsPeer;

import com.workingdogs.village.Record;

public class CashInBankController {
	private static Calendar calendar;
	
	public CashInBankController(){}
	
	public static int getCurrentWeekOfTheYear(){
		calendar = Calendar.getInstance();
		String weekId = calendar.get(Calendar.YEAR)+""+calendar.get(Calendar.WEEK_OF_YEAR);
		int id = Integer.parseInt(weekId);
		return id;
	}
	
	//Beginning Balance
	public static int getBeginningBalance(int weekId){
		String query = "SELECT totalCashInBankBalance from report WHERE reportId=" + (weekId-1);
		int beginningBalance;
		
		try{		
			List<Record> records2 = (List<Record>)ReportPeer.executeQuery(query);
			Record record2 = ((List<Record>)records2).get(0);
			beginningBalance = record2.getValue(1).asInt();
		}catch(Exception e){
			beginningBalance = 0;
		}
		
		return beginningBalance;
	}
	
	//Total Sum of Collections
	public static int getTotalSumOfCollections(int weekId){
		String query1 = "SELECT SUM(amount) FROM collectioncash WHERE weekId=" + weekId;
		String query2 = "SELECT SUM(amount) FROM collectioncheck WHERE weekId=" + weekId;
		int totalSumOfCollections;
		int sumOfCollectionCash;
		int sumOfCollectionCheck;
		
		//for Collection Cash
		try{
			List<Record> records1 = (List<Record>)CollectionCashPeer.executeQuery(query1);
			Record record1 = ((List<Record>)records1).get(0);
			sumOfCollectionCash = record1.getValue(1).asInt();			
		}
		catch(Exception e){
			sumOfCollectionCash = 0;
		}
		
		//for Collection Check
		try{		
			List<Record> records2 = (List<Record>)CollectionCheckPeer.executeQuery(query2);
			Record record2 = ((List<Record>)records2).get(0);
			sumOfCollectionCheck = record2.getValue(1).asInt();
		}catch(Exception e){
			sumOfCollectionCheck = 0;
		}
		
		totalSumOfCollections = sumOfCollectionCash + sumOfCollectionCheck;
		
		return totalSumOfCollections;
	}
	
	//Bank Interest
	public static int getBankInterest(int weekId){
		String query = "SELECT bankInterest from report where reportId=" + weekId;
		int bankInterest;
		
		try{
			List<Record> records = (List<Record>)ReportPeer.executeQuery(query);
			Record record = ((List<Record>)records).get(0);
			bankInterest = record.getValue(1).asInt();
			return bankInterest;
		}catch(Exception e){
			return 0;
		}
	}
	
	public static boolean isNumeric(String input){ //checks if number
		try 
		{
			 char[] ch = input.toCharArray(); 
			 for(int i = 0; i < input.length();i++) {
		            if( Character.isDigit(ch[i]) ) 
		            	continue; 
		            else return false;
			 }
			 return true;
		}catch (NumberFormatException nfe) {
			nfe.printStackTrace();
			return false;
		}
	
	}
	
	//Total Sum of Withdrawals
	public static int getTotalSumOfWithdrawals(int weekId){
		String query = "SELECT SUM(amount) FROM withdrawals WHERE weekId=" + weekId;
		int totalSumOfWithdrawals;
		
		try{
			List<Record> records = (List<Record>)WithdrawalsPeer.executeQuery(query);
			Record record = ((List<Record>)records).get(0);
			totalSumOfWithdrawals = record.getValue(1).asInt();
			return totalSumOfWithdrawals;
		}catch(Exception e){
			return 0;
		}
	}
	
	//Total Cash in Bank Balance
	public static int getTotalCashInBankBalance(int weekId){
		int beginningBalance = getBeginningBalance(weekId);
		int totalSumOfCollections = getTotalSumOfCollections(weekId);
		int bankInterest = getBankInterest(weekId);
		int totalSumOfWithdrawals = getTotalSumOfWithdrawals(weekId);
		int totalCashInBankBalance;
		
		totalCashInBankBalance = beginningBalance + totalSumOfCollections + bankInterest - totalSumOfWithdrawals;
		
		return totalCashInBankBalance;
	}
	
	public static void recomputeTotalCashInBankBalance(int weekId, int bankInterest){
		ReportPeer.updateBankInterest(weekId,bankInterest);
		ReportPeer.updateCashInBankForWeek(weekId,getBeginningBalance(weekId),getTotalSumOfCollections(weekId),bankInterest,getTotalSumOfWithdrawals(weekId),getTotalCashInBankBalance(weekId));
	}
}